/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.edu.utn.frsf.ofa.javaee7.tp.ejemplo.ecompras.controller;

import ar.edu.utn.frsf.ofa.javaee7.tp.ejemplo.ecompras.modelo.Gerente;
import ar.edu.utn.frsf.ofa.javaee7.tp.ejemplo.ecompras.util.BaseProduccion;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

/**
 *
 * @author Martin
 */
@Named
@ConversationScoped
@Transactional(Transactional.TxType.REQUIRES_NEW)
public class GerenteController implements Serializable {
    
    @PersistenceContext(unitName = "ecompras_PU")
    private EntityManager em;
    
    private Gerente gerente;

    @Inject
    private Conversation conversation;

    public void beginConversation() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }

    public void endConversation() {
        if (!conversation.isTransient()) {
            conversation.end();
        }
    }

    @PostConstruct
    public void init() {
        Logger.getLogger("GerenteController").log(Level.INFO, " INICIA EL BACK BEAN ");
    }

    /**
     * @return the gerente
     */
    public Gerente getGerente() {
        return gerente;
    }

    /**
     * @param gerente the gerente to set
     */
    public void setGerente(Gerente gerente) {
        this.gerente = gerente;
    }

    public String nuevo() {
        beginConversation();
        this.gerente = new Gerente();
        return null;
    }

    public String guardar() {
        this.em.persist(this.gerente);
        endConversation();
        return null;
    }

    public String editar() {
        this.em.merge(this.gerente);
        endConversation();
        return null;
    }

    public String borrar() {
        this.em.remove(this.gerente);
        endConversation();
        return null;
    }

    /**
     * @return the em
     */
    public EntityManager getEm() {
        return em;
    }

    /**
     * @param em the em to set
     */
    public void setEm(EntityManager em) {
        this.em = em;
    }

    /**
     * @return the conversation
     */
    public Conversation getConversation() {
        return conversation;
    }

    /**
     * @param conversation the conversation to set
     */
    public void setConversation(Conversation conversation) {
        this.conversation = conversation;
    }

}
